package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.MemberDao;
import com.itheima.dao.OrderDao;
import com.itheima.dao.ReportDao;
import com.itheima.service.ReportService;
import com.itheima.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * 运营数据统计服务
 */
@Service(interfaceClass = ReportService.class)
@Transactional
public class ReportServiceImpl implements ReportService {

    @Autowired
    private ReportDao reportDao;
    @Autowired
    private MemberDao memberDao;
    @Autowired
    private OrderDao orderDao;
    //查询运营数据
    public Map<String, Object> getBusinessReportData() throws Exception{
        //报表日期
        String today = DateUtils.parseDate2String(DateUtils.getToday());
        //获得本周一日期
        String thisWeekMonday = DateUtils.parseDate2String(DateUtils.getThisWeekMonday());
        //获得本月第一天日期
        String firstDay4ThisMonth = DateUtils.parseDate2String(DateUtils.getFirstDay4ThisMonth());

        //本日新增会员数
        Integer todayNewMember = memberDao.findMemberCountByDate(today);
        //总会员数
        Integer totalMember = memberDao.findMemberTotalCount();
        //本周新增会员数
        Integer thisWeekNewMember = memberDao.findMemberCountAfterDate(thisWeekMonday);
        //本月新增会员数
        Integer thisMonthNewMember = memberDao.findMemberCountAfterDate(firstDay4ThisMonth);

        //今日预约数
        Integer todayOrderNumber = orderDao.findOrderCountByDate(today);
        //本周预约数
        Integer thisWeekOrderNumber = orderDao.findOrderCountAfterDate(thisWeekMonday);
        //本月预约数
        Integer thisMonthOrderNumber = orderDao.findOrderCountAfterDate(firstDay4ThisMonth);
        //今日到诊数
        Integer todayVisitsNumber = orderDao.findVisitsCountByDate(today);
        //本周到诊数
        Integer thisWeekVisitsNumber = orderDao.findVisitsCountAfterDate(thisWeekMonday);
        //本月到诊数
        Integer thisMonthVisitsNumber = orderDao.findVisitsCountAfterDate(firstDay4ThisMonth);

        //热门套餐查询
        List<Map> hotSetmeal = orderDao.findHotSetmeal();

        Map<String,Object> result = new HashMap<>();
        result.put("reportDate",today);
        result.put("todayNewMember",todayNewMember);
        result.put("totalMember",totalMember);
        result.put("thisWeekNewMember",thisWeekNewMember);
        result.put("thisMonthNewMember",thisMonthNewMember);
        result.put("todayOrderNumber",todayOrderNumber);
        result.put("thisWeekOrderNumber",thisWeekOrderNumber);
        result.put("thisMonthOrderNumber",thisMonthOrderNumber);
        result.put("todayVisitsNumber",todayVisitsNumber);
        result.put("thisWeekVisitsNumber",thisWeekVisitsNumber);
        result.put("thisMonthVisitsNumber",thisMonthVisitsNumber);
        result.put("hotSetmeal",hotSetmeal);
        return result;
    }

    //根据月查询预约
    public List<Integer> findOrderCount1ByMonths(List<String> months) {
        //将12个月以前的预约数提取出来
        String date12 =months.get(0)+".31";
        Integer count12 = reportDao.findOrderCountBeforeDate1(date12);

        months.remove(0);

        List<Integer> orderCount = new ArrayList<>();
        for (String month : months) {
            String date = month + ".31";//2018.05.31
            Integer count = reportDao.findOrderCountBeforeDate1(date);
            //取一个值,目的是后面要减去这个数
            Integer temp=count12;
            //将count12重新赋值
            count12=count;
            //添加进集合的数是当月的数
            count=count-temp;

            orderCount.add(count);
        }

        return orderCount;
    }

    //根据月查询到诊
    public List<Integer> findOrderCount2ByMonths(List<String> months) {
        String date12 =months.get(0)+".31";
        Integer count12 = reportDao.findOrderCountBeforeDate2(date12);

        months.remove(0);

        List<Integer> orderCount = new ArrayList<>();

        for (String month : months) {
            String date = month + ".31";//2018.05.31
            Integer count =reportDao.findOrderCountBeforeDate2(date);
            //取一个值,目的是后面要减去这个数
            Integer temp=count12;
            //将count12重新赋值
            count12=count;
            //添加进集合的数是当月的数
            count=count-temp;

            orderCount.add(count);
        }

        return orderCount;
    }

    //根据年份查询预约和到诊人数,还有日期的集合
    public Map<String, Object> findOrderByYear(String year) {
        Map<String, Object> map=new HashMap<>();



       List<Integer> list1=new ArrayList<>();
       List<Integer> list2=new ArrayList<>();
       List<String> list3=new ArrayList<>();
        for (int i = 1; i <10 ; i++) {
            Map<String,Object> map1= new HashMap<>();
            String month=year+"-0"+i;
            String date1=year+"-0"+i+"-01";
            String date2=year+"-0"+i+"-31";
            map1.put("start",date1);
            map1.put("end",date2);
            Integer orderByYear = reportDao.findOrderByYear(map1);
            list1.add(orderByYear);
            Integer comeByYear = reportDao.findComeByYear(map1);
           list2.add(comeByYear);
            list3.add(month);
        }
        for (int i = 10; i <=12 ; i++) {
            Map<String,Object> map1= new HashMap<>();
            String month=year+"-"+i;
            String date1=year+"-"+i+"-01";
            String date2=year+"-"+i+"-31";
            map1.put("start",date1);
            map1.put("end",date2);
            Integer orderByYear = reportDao.findOrderByYear(map1);
            list1.add(orderByYear);
            Integer comeByYear = reportDao.findComeByYear(map1);
            list2.add(comeByYear);
            list3.add(month);
        }
        map.put("date",list3);
        map.put("order",list1);
        map.put("come",list2);

        return map;
    }

    //统计套餐收入
    public List<Map<String, Object>> findIncomeCount() {
        return reportDao.findIncomeCount();
    }

    public List<Map<String, Object>> findIncomeCount2(Map map) {
        return reportDao.findIncomeCount2(map);
    }

   //统计不同年龄段人数
    public Integer findByAge(Map<String, Object> map) {
        return reportDao.findByAge(map);
    }

    //根据日期范围分别查找每天的 预约和到诊map集合 封装成list集合再封装成map集合
    public Map<String, Object> findByDate(List<String> list) {

        Map<String,Object> map=new HashMap<>();
        List<Integer> list1= new ArrayList<>();
        List<Integer> list2= new ArrayList<>();

        for (String date:list){
            Integer byDate1 = reportDao.findByDate1(date);
           list1.add(byDate1);

            Integer byDate2 = reportDao.findByDate2(date);
            list2.add(byDate2);
        }
        map.put("orderCount",list1);
        map.put("comeCount",list2);
        map.put("Date",list);

        return map;

    }



 /*   //根据日期范围查找预约数
    public List<Map<String, Object>> findByDate1(Map map) {
        return reportDao.findByDate1(map);
    }

    //根据日期范围查找到诊数
    public List<Map<String, Object>> findByDate2(Map map) {
        return reportDao.findByDate2(map);
    }*/


}
